package com.xckj.cycling.utils

import android.app.Activity
import android.net.Uri
import android.text.TextUtils
import android.util.Log
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.lifecycleScope
import com.bumptech.glide.Glide
import com.facebook.CallbackManager.Factory.create
import com.facebook.FacebookCallback
import com.facebook.FacebookException
import com.facebook.FacebookSdk.sdkInitialize
import com.facebook.share.Sharer
import com.facebook.share.model.ShareLinkContent
import com.facebook.share.model.ShareMediaContent
import com.facebook.share.model.SharePhoto
import com.facebook.share.widget.ShareDialog
import com.xckj.cycling.R
import com.xckj.cycling.context.AppContext
import com.xckj.cycling.context.AppContext.Companion.globalContext
import com.xckj.cycling.framework.BaseActivityKt
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.io.File
import java.util.concurrent.ExecutionException

object FacebookShareUtils {
    fun shareFaceBookWeb(context: Activity?, url: String?) {
        if (TextUtils.isEmpty(url)) {
            return
        }
        val shareContent: ShareLinkContent = ShareLinkContent.Builder()
            .setContentUrl(Uri.parse(url))
            .build()
        val shareDialog = ShareDialog(context!!)
        shareDialog.show(shareContent, ShareDialog.Mode.AUTOMATIC)
    }

    fun shareFaceBookImage(activity: Activity?, primaryContent: String?, url: String?) {
        sdkInitialize(globalContext())
        val shareContent: ShareLinkContent = ShareLinkContent.Builder()
            .setQuote(primaryContent)
            .setContentUrl(Uri.parse(url))
            .build()
        val shareDialog = ShareDialog(activity!!)
        shareDialog.show(shareContent, ShareDialog.Mode.AUTOMATIC)
    }

    fun shareImages(activity: BaseActivityKt, urls: Array<String>) {
        val install = PackageUtils.isAppInstalled(activity, "com.facebook.katana")
        if (!install) {
            ToastUtils.showToast(activity.getString(R.string.please_install_the_facebook_client_to_share))
            return
        }
        sdkInitialize(globalContext())
        activity.lifecycleScope.launch {
            activity.showLoading()
            val u = if (urls.size > 6) urls.toMutableList().subList(0, 6).toTypedArray() else urls
            val shareContent = ShareMediaContent.Builder()
            for (i in u.indices) {
                val path = urlToFile(u[i])
                val sharePhoto2: SharePhoto = SharePhoto.Builder()
                    .setImageUrl(Uri.fromFile(path))
                    .build()
                shareContent.addMedium(sharePhoto2)
            }
            val shareDialog = ShareDialog(activity)
            val callbackManager = create()
            shareDialog.registerCallback(callbackManager, object : FacebookCallback<Sharer.Result> {
                override fun onSuccess(result: Sharer.Result) {
                    Log.i("Share", result.toString())
//                    ToastUtils.debug("分享成功")
                }

                override fun onCancel() {
                    Log.i("Share", "cancel")
//                    ToastUtils.debug("分享取消 ")

                }

                override fun onError(e: FacebookException) {
                    Log.e("Share", "error", e)
//                    ToastUtils.debug("分享失败 " + e.message)
                }
            })
            shareDialog.show(shareContent.build(), ShareDialog.Mode.AUTOMATIC)
            activity.hideDialog()
        }

    }

    suspend fun urlToFile(url: String): File {
        return withContext(Dispatchers.IO) {
            Glide.with(AppContext.globalContext()).downloadOnly().load(url).submit()
                .get()
        }
    }
}